package org.vergil.core.thread.producerconsumer;

import java.util.Random;

/**
 * Created by vergil on 17-3-18.
 */
public class Consumer implements Runnable{

    private Business business;

    Consumer(Business business){
        this.business = business;
    }

    @Override
    public void run() {

        while (true) {
            if (!business.getQueue().isEmpty()){
                Random random = new Random();
                int num = random.nextInt(10);
                try {
                    synchronized (this) {
                        business.getProducts(num);
                    }
                    System.out.println(Thread.currentThread().getName() + " get " + num + " Product");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
